<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> {{ myFormTitle }} </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-form ref="form" label-width="80px" :inline="true">
                    <el-form-item label="筛选条件">
                        <el-input
                            placeholder="柜台编号、名称或备注模糊筛选"
                            class="handle-input mr10"
                            v-model="query.FilterPara"
                        ></el-input>
                    </el-form-item>
                    <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                    <el-button type="primary" icon="el-icon-plus" @click="handleCreate">新建</el-button>
                </el-form>
            </div>
            <el-table
                ref="multipleTable"
                :data="tableData"
                highlight-current-row
                style="width: 100%"
                fit
                border
                @selection-change="handleSelectionChange"
            >
                <!-- <el-table-column type="selection" width="55" > </el-table-column> -->
                <el-table-column label="柜台id" align="center" v-if="show" prop="FCounterID">
                    <template slot-scope="scope">{{ scope.row.FCounterID }}</template>
                </el-table-column>
                <el-table-column label="柜台名称" align="center" prop="FCounterName">
                    <template slot-scope="scope">{{ scope.row.FCounterName }}</template>
                </el-table-column>
                <el-table-column label="柜台编号" align="center" prop="FCounterNo">
                    <template slot-scope="scope">{{ scope.row.FCounterNo }}</template>
                </el-table-column>
                <el-table-column label="操作" width="250" align="center" fixed="right">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)"
                            >删除</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.FPageIndex"
                    :page-size="query.FPageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="新建柜台" :visible.sync="editVisible" width="30%" @close="formClose">
            <el-form ref="form" :model="form" label-width="100px"
                ><el-form-item label="柜台编号" prop="FCounterNo" :rules="rulesadd.FCounterNo">
                    <el-input v-model="form.FCounterNo"></el-input>
                </el-form-item>
                <el-form-item label="柜台名称" prop="FCounterName" :rules="rulesadd.FCounterName">
                    <el-input v-model="form.FCounterName"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 编辑弹出框 -->
        <el-dialog :title="showDialogTitle" :visible.sync="dialogVisible" width="30%" @close="editFormClose">
            <el-form ref="editForm" :model="editForm" label-width="100px">
                <el-form-item label="柜台id" v-if="show"> <el-input v-model="editForm.FCounterID"></el-input> </el-form-item>
                <el-form-item label="柜台名称"> <el-input v-model="editForm.FCounterName"></el-input> </el-form-item>
                <el-form-item label="柜台编号"> <el-input v-model="editForm.FCounterNo"></el-input> </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { fetchDataFromApi } from '../../api/apiService';
import { Loading } from 'element-ui';
export default {
    inject: ['reload'],
    name: 'basetable',
    data() {
        return {
            show: false,
            currentPage1: 1,
            editVisible: false,
            dialogVisible: false,
            showDialogTitle: '',
            myFormTitle: '柜台管理 / 数据清单',
            pageTotal: 0,
            query: {
                FilterPara: '',
                FPageIndex: 1,
                FPageSize: 10
            },
            form: {
                FCounterName: '',
                FCounterNo: ''
            },
            editForm: {
                FCounterID: '',
                FCounterName: '',
                FCounterNo: ''
            },
            tableData: [],
            multipleSelection: [],
            rulesadd: {
                FCounterNo: [{ required: true, message: '请输入柜台编号', trigger: 'blur' }],
                FCounterName: [{ required: true, message: '请输入柜台名称', trigger: 'blur' }]
            }
        };
    },

    created() {
        this.getData();
    },
    methods: {
        //弹窗关闭删除
        formClose() {
            this.$refs.form.clearValidate();
            this.form = {
                FCounterName: '',
                FCounterNo: ''
            };
        },
        editFormClose() {
            this.editForm = {
                FCounterID: '',
                FCounterName: '',
                FCounterNo: ''
            };
        },
        handleSizeChange(val) {
            // console.log(`每页 ${val} 条`);
        },
        handleCurrentChange(val) {
            // console.log(`当前页: ${val}`);
        },
        // 触发新增按钮
        handleCreate() {
            this.editVisible = true;
        },
        //表中内容修改时用
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },

        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'FPageIndex', 1);
            //this.query.FPageIndex = 1;
            this.getData();
        },
        //获取数据
        getData() {
            this.listLoading = true;
            this.query.ActionCode = 'SearchCounter';
            this.query.Token = localStorage.getItem('ms_token');
            fetchDataFromApi(this.query).then(res => {
                if (res != undefined && res != null && res != '') {
                    // console.log(res);
                    this.tableData = res.data.items;
                    this.pageTotal = res.data.total;
                }
                this.listLoading = false;
            });
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            // console.log(JSON.parse(JSON.stringify(row)));
            // console.log(row);
            this.$confirm('确定要删除吗？', '提示', { type: 'warning' })
                .then(() => {
                    if (row != undefined && row != null && row != '') {
                        var myRowShopFID = row.FCounterID;

                        var querypars = {};
                        querypars.ActionCode = 'DeleteCounter';
                        querypars.Token = localStorage.getItem('ms_token');
                        querypars.FCounterID = myRowShopFID;

                        fetchDataFromApi(querypars).then(res => {
                            if (res != undefined && res != null && res != '') {
                                if (res.Message == 'OK') {
                                    this.$message.success(res.Info);
                                    this.getData();
                                } else {
                                    this.$message.error(res.Info);
                                }
                            }
                        });
                    }
                })
                .catch(() => {});
        },
        // 编辑操作
        handleEdit(index, row) {
            this.showDialogTitle = '编辑柜台';
            this.dialogVisible = true;
            this.editForm = Object.assign({}, row);
        },
        //保存新增
        saveAdd() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.form.ActionCode = 'AddCounter';
                    this.form.Token = localStorage.getItem('ms_token');
                    this.form.FUserNo = localStorage.getItem('ms_userno');
                    fetchDataFromApi(this.form).then(res => {
                        if (res != undefined && res != null && res != '') {
                            // console.log(res);
                            if (res.Message == 'OK') {
                                this.$message.success(res.Info);
                                this.getData();
                            } else {
                                this.$message.error(res.Info);
                            }
                        }

                        this.editVisible = false;
                    });
                }
            });
        },

        //保存编辑
        saveEdit() {
            this.$refs.editForm.validate(valid => {
                if (valid) {
                    if (this.editForm.FCounterID == undefined || this.editForm.FCounterID == null || this.editForm.FCounterID == '')
                        this.editForm.ActionCode = 'AddCounter';
                    else this.editForm.ActionCode = 'UpdateCounter';

                    this.editForm.Token = localStorage.getItem('ms_token');
                    this.editForm.FUserNo = localStorage.getItem('ms_userno');

                    fetchDataFromApi(this.editForm).then(res => {
                        if (res != undefined && res != null && res != '') {
                            if (res.Message == 'OK') {
                                this.$message.success(res.Info);
                                this.getData();
                            } else {
                                this.$message.error(res.Info);
                            }
                        }
                        this.dialogVisible = false;
                    });
                }
            });
        },

        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'FPageIndex', val);
            this.getData();
        },

        handlePageChangeDayTarget(val) {
            this.$set(this.formDay, 'FPageIndex', val);
            this.loadDatTableData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 220px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>





